package cn.wildfire.chat.kit.conversation.wallet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wildfire.chat.kit.AppService;
import cn.wildfire.chat.kit.R;
import cn.wildfire.chat.kit.R2;
import cn.wildfire.chat.kit.conversation.ConversationFragment;
import cn.wildfire.chat.kit.entry.RedRecordResult;
import cn.wildfire.chat.kit.entry.RedResult;
import cn.wildfire.chat.kit.entry.WalletResult;
import cn.wildfire.chat.kit.viewmodel.MessageViewModel;
import cn.wildfirechat.message.Message;
import cn.wildfirechat.message.TextMessageContent;
import cn.wildfirechat.message.notification.TipNotificationContent;

public class PackageDialog extends Dialog {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.tvReceiveNum)
    TextView tvReceiveNum;
    @BindView(R2.id.tvRecord)
    TextView tvRecord;
    @BindView(R2.id.progressBar)
    ProgressBar progressBar;
    @BindView(R2.id.btReceive)
    View btReceive;
    private RedResult redResult;
    private String currentUid;

    public PackageDialog(@NonNull Context context) {
        super(context);
    }

    public PackageDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_packge);
        ButterKnife.bind(this);
        SharedPreferences sp = getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
        currentUid = sp.getString("id", "");
    }

    public void setPackageInfo(String userName,RedResult redResult){
        this.redResult = redResult;
        tvTitle.setText(userName+" 发来的红包");
        receiveRecord(redResult.getId());
    }
    private void receiveRecord(String redId){
        if (TextUtils.isEmpty(redId)){
            return;
        }
        AppService.Instance().getRedRecord(redId,new AppService.RedRecordCallback() {
            @Override
            public void onUiSuccess(RedRecordResult data) {
                if (!isShowing()) {
                    return;
                }
                progressBar.setVisibility(View.GONE);
                if (data.getRecords()==null){
                    btReceive.setVisibility(View.VISIBLE);
                    return;
                }
                if (data.getRecords().size()==0){
                    btReceive.setVisibility(View.VISIBLE);
                    return;
                }
                for (RedRecordResult.RecordEntry record : data.getRecords()) {
                    if (record.getRuid().equals(currentUid)) {
                        btReceive.setVisibility(View.INVISIBLE);
                        tvReceiveNum.setText("已领取￥："+record.getAmount());
                    }else {
                        btReceive.setVisibility(View.VISIBLE);
                    }
                }
            }

            @Override
            public void onUiFailure(int code, String msg) {
                if (!isShowing()) {
                    return;
                }
                progressBar.setVisibility(View.GONE);
            }
        });
    }
    private void receiveRed(){
        if (redResult==null){
            return;
        }
        AppService.Instance().postReceiveRed(redResult.getId(),new AppService.RedCallback() {
            @Override
            public void onUiSuccess(RedResult data) {
                if (!isShowing()) {
                    return;
                }
                ToastUtils.showShort("领取成功");
                btReceive.setVisibility(View.INVISIBLE);
                ConversationFragment.getInstance().receiveRedTip();
                dismiss();
            }

            @Override
            public void onUiFailure(int code, String msg) {
                if (!isShowing()) {
                    return;
                }
                ToastUtils.showShort("领取失败"+msg+"code:"+code);
            }
        });
    }
    private void sendReceiveTipMessage(){
//        MessageViewModel messageViewModel = ViewModelProviders.of(getOwnerActivity()).get(MessageViewModel.class);
//        TipNotificationContent content = new TipNotificationContent();
//        content.tip =redResult.getRuid()+ "领取了红包";
//        Intent intent = new Intent();
//        intent.putExtra("content",content);
//        messageViewModel.sendMessage(conversation, content);
    }
    @OnClick(R2.id.btReceive)
    public void onReceiveRed(){
        receiveRed();
    }
    @OnClick(R2.id.tvRecord)
    public void onReceiveRecord(){
        Intent intent = new Intent(getContext(),PackgeRecordActivity.class);
        intent.putExtra("redId",redResult.getId());
        getContext().startActivity(intent);
    }
}